package com.bcd.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.bcd.beans.Article;

public class Searcher {
	private DbManager db;
	
	public Searcher(){
		db = DbManager.getInstance();
	}
	
	public ArrayList<Article> Search(String term, String type){
		
		ArrayList<Article> articleList = new ArrayList<Article>();
		ArticleManager manager = new ArticleManager();
		Article article = null;
		ResultSet rs;
		
		String query = "SELECT article_id FROM articles WHERE article_title LIKE '%" + term + "%' ";
		
		if(!type.equals("ALL")){
			query += "AND article_type = '" + type + "'";
		}
		
		rs = db.ExecuteSelect(query);
		
		try {
			while(rs.next()){
				
				article = manager.GetArticle(rs.getInt("article_id"));
				articleList.add(article);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return articleList;
	}
}
